##################################################
## Replication materials for 
## "Competent legislators or mere pawns? 
## Experimental evidence of attitudes toward gender quota politicians"
## by Carolyn Barnett, Alexandra Blackman, and Marwa Shalaby

## 1. Setup and Information

## This file loads packages, environment parameters, and datasets

# SET WORKING DIRECTORY ---------------

# set to location of replication folder on local device
# e.g. setwd("~quota-rep-files/")


# REQUIRED PACKAGES --------------------

need <- c("plyr", "tidyverse", "broom", "stargazer", 
          "xtable", "modelsummary", "haven")

have <- need %in% rownames(installed.packages()) # installed packages
if(any(!have)) install.packages(need[!have]) # install missing packages
invisible(lapply(need, library, character.only=T)) # load needed packages

# ENVIRONMENT -----------------

# Set tables to show "NA" by default
table = function (..., useNA = 'ifany') base::table(..., useNA = useNA)  

# Common plot parameters
theme_cb <- function () { 
  theme_bw(base_size=12, base_family="") %+replace% 
    theme(
      # change stuff here
      panel.background=element_blank(),
      legend.background=element_rect(fill="transparent", colour=NA),
      legend.key=element_rect(fill="transparent", colour=NA),
      axis.text.x=element_text(size=10),
      axis.title.x=element_text(size=12,
                                margin = margin(t = 10, r = 0, b = 0, l = 0)),
      axis.text.y=element_text(size=10), 
      axis.title.y=element_text(size=12, angle=90,
                                margin = margin(t = 0, r = 10, b = 0, l = 0)),
      plot.title=element_text(size=18, color="black",
                              margin = margin(t = 0, r = 0, b = 10, l = 0)),
      plot.subtitle=element_text(size=14)
    )
}

# create vector of outcome names for plots
outcome_names <- as_labeller(c(`vignette_competence` = "Competence", 
                               `vignette_pawnlike` = "Pawn-like"))


# CREATE SUMMARIZING FUNCTION ------------

## Gives count, mean, standard deviation, standard error of the mean, and confidence interval (default 95%).
##   data: a data frame.
##   measurevar: the name of a column that contains the variable to be summarized
##   groupvars: a vector containing names of columns that contain grouping variables
##   na.rm: a boolean that indicates whether to ignore NA's
##   conf.interval: the percent range of the confidence interval (default is 95%)
summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=T,
                      conf.interval=.95, .drop=TRUE) {
  library(plyr)
  
  # New version of length which can handle NA's: if na.rm==T, don't count them
  length2 <- function (x, na.rm=T) {
    if (na.rm) sum(!is.na(x))
    else       length(x)
  }
  
  # This does the summary. For each group's data frame, return a vector with
  # N, mean, and sd
  datac <- ddply(data, groupvars, .drop=.drop,
                 .fun = function(xx, col) {
                   c(N    = length2(xx[[col]], na.rm=na.rm),
                     mean = mean   (xx[[col]], na.rm=na.rm),
                     sd   = sd     (xx[[col]], na.rm=na.rm)
                   )
                 },
                 measurevar
  )
  
  # Rename the "mean" column    
  datac <- plyr::rename(datac, c("mean" = measurevar))
  
  datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean
  
  # Confidence interval multiplier for standard error
  # Calculate t-statistic for confidence interval: 
  # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
  ciMult <- qt(conf.interval/2 + .5, datac$N-1)
  datac$ci <- datac$se * ciMult
  
  return(datac)
}




# LOAD DATA -------------------

## Quota experiment dataset --------
# (needed for running most code files)
morvign <- read_rds("quota_experiment.rds")

## Summary statistics variables from full survey sample (n = 1800) --------
# (needed only to run file 03_summary_stats.R)
m_summary <- read_rds("summary_stats_1800.rds")

## Morocco data from other surveys for Appendix Table A.1 --------
# (needed only to run file 03_summary_stats.R)

# Afrobarometer
af9_mor <- read_rds("af9_mor.rds")
# Arab Barometer
ab7_mor <- read_rds("ab7_mor.rds")
# World Values Survey
wvs7_mor <- read_rds("wvs7_mor.rds")









